What is emoji-regex?
The emoji-regex npm package provides a regular expression to match all Emoji-only symbols as per the Unicode Standard. This package is useful for processing text that contains emojis, allowing developers to identify, validate, or manipulate emojis within strings.
What are emoji-regex's main functionalities?
Matching emojis in a string
This code sample demonstrates how to use emoji-regex to find all emojis in a given string. The regex is executed against a string containing emojis, and it will match each emoji character or sequence.
/(emojiRegex())/g.exec('I ❤️ emoji!')
Replacing emojis in a string
This code sample shows how to replace all emojis in a string with a specific word or character. In this case, every emoji in the string is replaced with the word 'emoji'.
'I ❤️ emoji!'.replace(emojiRegex(), 'emoji')
Counting emojis in a string
This code sample is used to count the number of emojis in a string. The match function returns an array of all emojis found, and the length property of the array indicates the total number of emojis.
'👍👍🏿'.match(emojiRegex()).length
Other packages similar to emoji-regex
emojione
Emojione is a package that provides emoji conversion and detection. It includes functionalities to convert unicode emojis to shortnames, images, and vice versa. It is more feature-rich compared to emoji-regex, which focuses solely on regex matching.
node-emoji
Node-emoji is a package that allows for simple emoji handling and conversion in Node.js. It can convert emoji names to unicode and unicode to names, similar to emojione but with a simpler API. Unlike emoji-regex, it does not provide a regex pattern for matching emojis.
emoji-aware
Emoji-aware is a package that splits strings into arrays of Unicode emoji and text. It is useful for separating emojis from other text, but it does not provide regex functionality like emoji-regex.
emoji-regex
emoji-regex offers a regular expression to match all emoji symbols (including textual representations of emoji) as per the Unicode Standard.
This repository contains a script that generates this regular expression based on the data from Unicode Technical Report #51. Because of this, the regular expression can easily be updated whenever new emoji are added to the Unicode standard.
Installation
Via npm:
npm install emoji-regex
In Node.js:
const emojiRegex = require('emoji-regex');
const text = `
\u{231A}: ⌚ default emoji presentation character (Emoji_Presentation)
\u{2194}\u{FE0F}: ↔️ default text presentation character rendered as emoji
\u{1F469}: 👩 emoji modifier base (Emoji_Modifier_Base)
\u{1F469}\u{1F3FF}: 👩🏿 emoji modifier base followed by a modifier
`;
const regex = emojiRegex();
let match;
while (match = regex.exec(text)) {
const emoji = match[0];
console.log(`Matched sequence ${ emoji } — code points: ${ [...emoji].length }`);
}
Console output:
Matched sequence ⌚ — code points: 1
Matched sequence ⌚ — code points: 1
Matched sequence ↔️ — code points: 2
Matched sequence ↔️ — code points: 2
Matched sequence 👩 — code points: 1
Matched sequence 👩 — code points: 1
Matched sequence 👩🏿 — code points: 2
Matched sequence 👩🏿 — code points: 2
To match emoji in their textual representation as well (i.e. emoji that are not Emoji_Presentation
symbols and that aren’t forced to render as emoji by a variation selector), require
the other regex:
const emojiRegex = require('emoji-regex/text.js');
Additionally, in environments which support ES2015 Unicode escapes, you may require
ES2015-style versions of the regexes:
const emojiRegex = require('emoji-regex/es2015/index.js');
const emojiRegexText = require('emoji-regex/es2015/text.js');
Author
License
emoji-regex is available under the MIT license.